package com.qianfeng.wuyang.himalaya.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.qianfeng.wuyang.himalaya.R;
import com.qianfeng.wuyang.himalaya.adapter.broadcastadapter.NationalRecyclerAdapter;
import com.qianfeng.wuyang.himalaya.bean.broadcastbean.NationEntity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class NetActivity extends AppCompatActivity implements View.OnClickListener, Callback<NationEntity>, SwipeRefreshLayout.OnRefreshListener {
    private RecyclerView recycler_NetActivity;
    private ImageView back;
    private NationalRecyclerAdapter nationalRecyclerAdapter;
    private int pageNum=1;
    private SwipeRefreshLayout refresh_net;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_net);
        initView();
        nationalRecyclerAdapter = new NationalRecyclerAdapter(this);
        //请求数据
        new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(BCNationalService.class)
                .getData(1,20,3).enqueue(this);
        recycler_NetActivity.setAdapter(nationalRecyclerAdapter);
        recycler_NetActivity.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //得到当前显示的最后一个item的view
                View lastChildView = recyclerView.getLayoutManager().getChildAt(recyclerView.getLayoutManager().getChildCount()-1);
                //得到lastChildView的bottom的坐标
                int lastChildBottom = lastChildView.getBottom();
                //得到Recyclerview的底部坐标减去底部padding值，也就是显示内容最底部的坐标
                int recyclerBottom =  recyclerView.getBottom()-recyclerView.getPaddingBottom();
                //通过这个lastChildView得到这个view当前的position值
                int lastPosition  = recyclerView.getLayoutManager().getPosition(lastChildView);
                if(lastPosition == recyclerView.getLayoutManager().getItemCount()-1 ) {
                    pageNum=pageNum+1;
                    loadData(pageNum);
                }
            }

        });
    }
    private void initView() {
        recycler_NetActivity = ((RecyclerView) findViewById(R.id.recycler_NetActivity));
        back = ((ImageView) findViewById(R.id.image_back));
        back.setOnClickListener(this);
        refresh_net = ((SwipeRefreshLayout) findViewById(R.id.refresh_net));
        //设置刷新的颜色渐变
        refresh_net.setColorSchemeResources(
                R.color.colorAccent,
                R.color.colorPrimary,
                R.color.tab_indicator,
                R.color.white
        );
        refresh_net.setOnRefreshListener(this);

        // 这句话是为了，第一次进入页面的时候显示加载进度条，调整进度条距离屏幕顶部的距离
        refresh_net.setProgressViewOffset(false, 0, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
                        .getDisplayMetrics()));
    }

    @Override
    public void onClick(View view) {
        finish();
    }


    @Override
    public void onResponse(Call<NationEntity> call, Response<NationEntity> response) {
        if (response.body().getResult() != null) {
            nationalRecyclerAdapter.addAll(response.body().getResult());
        }

    }

    @Override
    public void onFailure(Call<NationEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();

    }
    private void loadData(int page){
        new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(BCNationalService.class)
                .getData(page,20,1).enqueue(this);
    }

    @Override
    public void onRefresh() {
        nationalRecyclerAdapter = new NationalRecyclerAdapter(this);
        recycler_NetActivity.setAdapter(nationalRecyclerAdapter);
        loadData(1);
        refresh_net.setRefreshing(false);
    }
}
